﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ListaTelefonicaMEM.Controles
{
    public partial class LabelProgressoPadrao : UserControl
    {
        public LabelProgressoPadrao()
        {
            InitializeComponent();
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.AutoSize = false;
            ResizeManual();
        }

        //private properties
        private int NrDigitos;
        private int FMaximo;
        private int FValor;

        //public properties
        public LabelPadraoBarra LinkedLabelBarra { get; set; }
        public int Maximo
        {
            get
            {
                return FMaximo;
            }
            set
            {
                FMaximo = value;
                NrDigitos = FMaximo.ToString().Length + 1;
                lbMaximo.Text = FMaximo.ToString().PadLeft(NrDigitos);
                ResizeManual();
                lbMaximo.Refresh();
            }
        }

        public int Valor
        {
            get
            {
                return FValor;
            }
            set
            {
                FValor = value;
                lbValor.Text = FValor.ToString().PadLeft(NrDigitos);
                SetBarraProgresso();
                ResizeManual();
                this.Refresh();
            }
        }

        public override string Text
        {
            get
            {
                return lbTipo.Text;
            }
            set
            {
                lbTipo.Text = value;
                ResizeManual();
                lbTipo.Refresh();
            }
        }

        //métodos
        private void ResizeManual()
        {
            int Y = 0;

            int TipoWidth = lbTipo.Size.Width + 1;
            int ValorWidth = lbValor.Size.Width + 1;
            int DivisorWidth = lbDivisor.Size.Width + 1;
            int MaximoWidth = lbMaximo.Size.Width;
            int TextoWidth = (int)((TipoWidth + ValorWidth + DivisorWidth + MaximoWidth) / 2);
            int LabelWidth = (int)(this.Size.Width / 2);

            int XTipo = LabelWidth - TextoWidth;
            lbTipo.Location = new Point(XTipo, Y);

            int XValor = lbTipo.Location.X + TipoWidth;
            lbValor.Location = new Point(XValor, Y);

            int XDivisor = lbValor.Location.X + ValorWidth;
            lbDivisor.Location = new Point(XDivisor, Y);

            int XMaximo = lbDivisor.Location.X + DivisorWidth;
            lbMaximo.Location = new Point(XMaximo, Y);

            //this.Size = new Size(lbMaximo.Location.X + XMaximo, this.Size.Height);

        }


        private void SetBarraProgresso()
        {
            if (LinkedLabelBarra != null)
            {
                double inc = 100 / (double)Maximo;
                int val = (int)((inc * Valor)/2);
                string str = "";

                for (int i = 0; i < val; i++)
                {
                    str += "█";
                }

                LinkedLabelBarra.Text = str;
                LinkedLabelBarra.Refresh();
            }
        }
    }
}
